package com.lanou.musicbox.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.MainFragmentAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.utils.MyConfig;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;


/**
 * Created by 杨海宁 on 15/10/26.
 * 整体进入页
 */
public class MainFragmentAll extends BaseFragment implements
        OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    @Bind(R.id.locality_tab)
    RadioButton localityTab;
    @Bind(R.id.network_music_tab)
    RadioButton networkMusicTab;
    @Bind(R.id.network_radio_tab)
    RadioButton networkRadioTab;
    @Bind(R.id.seek_music_tab)
    RadioButton seekMusicTab;
    @Bind(R.id.vp_content)
    ViewPager vpContent;
    @Bind(R.id.rg_main_tab)
    RadioGroup rgMainTab;
    private MainFragmentAdapter adapter;
    private String b;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.radio_group_fill, null);
        ButterKnife.bind(this, v);
        return v;
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new MainFragmentAdapter(getChildFragmentManager());
        vpContent.setAdapter(adapter);
        vpContent.addOnPageChangeListener(this);
        rgMainTab.setOnCheckedChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                localityTab.setChecked(true);
                break;
            case 1:
                networkMusicTab.setChecked(true);
                break;
            case 2:
                networkRadioTab.setChecked(true);
                break;
            case 3:
                seekMusicTab.setChecked(true);
                break;

        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {

            case R.id.locality_tab:
                vpContent.setCurrentItem(0);
                break;
            case R.id.network_music_tab:
                vpContent.setCurrentItem(1);
                break;
            case R.id.network_radio_tab:
                vpContent.setCurrentItem(2);
                break;
            case R.id.seek_music_tab:
                vpContent.setCurrentItem(3);
                break;
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        MyConfig setting =new MyConfig(getActivity(),false);
        vpContent.setBackgroundResource(setting.getCurrentSkinResID());
    }

    public void onEvent(Events.FristEvent fristEvent){

        b=fristEvent.getMsg();
        if (b.equals("two")){
            vpContent.setCurrentItem(2);
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}